<?php
class Price extends CI_Model {

	function get_price($start,$limit,$sort='id',$dir='ASC')
    {
	   	$query = $this->db->query("SELECT * FROM price ORDER BY $sort $dir LIMIT $start,$limit");
	    //die("SELECT * FROM price ORDER BY $sort $dir LIMIT $start,$limit");
		return $query->result_array();
        
    }
    function get_range(){
    	$query = $this->db->query("SELECT * FROM price");
		return $query->result_array();
    }
	function get_by_number_of_applicant($number)
    {
	    $query = $this->db->query('SELECT * FROM price WHERE min_person<='.$number.' AND max_person>='.$number);
		return $query->row_array();
    }
	/*function get_all(){
    	$query = $this->db->query("SELECT * FROM price");
		return $query->result_array();
    }*/
	function get_total()
    {
	    $query = $this->db->query('SELECT count(*) as total FROM price');
		$row = $query->row_array();
        return $row['total'];
    }
    function insert_price($data)
    {
        $this->filterData($data);
        $this->db->insert('price', $data);
        return $this->db->insert_id()  ;
    }
	function get_by_id($id)
    {
	    $this->db->where('id',$id);
		$query = $this->db->get("price");
        return $query->row_array();
        
    }
    function update_price($data,$id)
    {
		$this->filterData($data);
        return $this->db->update('price', $data, array('id' => $id));
    }
    function delete_price($id){
    	$this->db->delete('price', array('id' => $id)); 
    }
	protected function filterData(&$data){
		foreach($data as $key=>$row){
			if (!$this->db->field_exists($key, 'price'))
			{
				unset($data[$key]);
			} 
		}
    }
}